<!DOCTYPE HTML>
<html  xmlns:th="http://www.thymeleaf.org">
<head th:include="_meta :: header">
<title>添加管理员 - 管理员管理 </title>
</head>
<body>
<div class="layui-fluid">
	<div class="layui-row">
		<form class="layui-form form form-horizontal" id="form-admin-add" action="#" th:action="@{/user/add-user}" >
			<input type="hidden" name="save" value="true"/>
			<div class="layui-form-item">
				<label for="username" class="layui-form-label">
					<span class="x-red">*</span>管理员
				</label>
				<div class="layui-input-inline">	
					<input type="text" id="username" name="username"  
						   autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">
					<span class="x-red">*</span>初始密码
				</label>
				<div class="layui-input-inline">
					<input type="password" id="password" name="password" 
						   autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">
					<span class="x-red">*</span>确认密码
				</label>
				<div class="layui-input-inline">
					<input type="password" id="password2" name="password2" 
						   autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">
					<span class="x-red">*</span>性别
				</label>
				<div class="layui-input-inline">
					<input type="radio" id="sex-1" name="sexType" value="1" title="男"  checked="checked">
					<input type="radio"  id="sex-0" name="sexType" value="0" title="女">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">
					<span class="x-red">*</span>是否启用
				</label>
				<div class="layui-input-inline">
					<input type="radio" id="qd" name="available" value="1" title="启用"  checked="checked">
					<input type="radio"  id="nqd" name="available" value="0" title="不启用">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">
					手机
				</label>
				<div class="layui-input-inline">
					<input type="text" id="tel" name="tel"  lay-verify="phone"
						   autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">
					邮箱
				</label>
				<div class="layui-input-inline">
					<input type="text" id="email" name="email" lay-verify="email"
						   autocomplete="off" class="layui-input">
				</div>
			</div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    角色
                </label>
                <div class="layui-input-inline">
                    <span  th:each="role : ${roleList}">
                        <input type="radio" th:value="${role.id}" value="" name="userRoles" th:title="${role.name}">
                    </span>
                </div>
            </div>
			<div class="layui-form-item">
				<label class="layui-form-label">
				</label>
				<button  class="layui-btn" id="subbtn" type="submit">
					增加
				</button>
			</div>
		</form>
	</div>
</div>

<!--_footer 作为公共模版分离出去--> 
<div th:replace="_footer :: footerjs"></div>
 <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" th:src="@{/lib/jquery.validation/1.14.0/jquery.validate.js}" src="lib/jquery.validation/1.14.0/jquery.validate.js"></script> 
<script type="text/javascript" th:src="@{/lib/jquery.validation/1.14.0/validate-methods.js}" src="lib/jquery.validation/1.14.0/validate-methods.js"></script> 
<script type="text/javascript" th:src="@{/lib/jquery.validation/1.14.0/messages_zh.js}"  src="lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
$(function(){
    layui.use(['form', 'layer'],
        function() {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;
        });
	
	$("#form-admin-add").validate({
		rules:{
			username:{
				required:true,
				minlength:4,
				maxlength:16
			},
			password:{
				required:true,
			},
			password2:{
				required:true,
				equalTo: "#password"
			},
			sexType:{
				required:true,
			},
			tel:{
				isPhone:true,
			},
			email:{
				email:true,
			},
		},
		onkeyup:false,
		success:"valid",
		submitHandler:function(form){
			$(form).ajaxSubmit({
				type: 'post',
				url: "/user/add-user" ,
				success: function(data){
					if(data == 1){
						//禁止重复提交
						$('#subbtn').attr("type","button");
						layer.msg('添加成功!',{icon:1,time:1000},function(){
							var index = parent.layer.getFrameIndex(window.name);
							parent.location.reload();
							parent.layer.close(index);
						});
					}else if(data == 2){
						layer.msg("用户已存在");
						return false;
					}else{
						layer.msg('增加失败!',{icon:1,time:1000});
					}
				},
                error: function(XmlHttpRequest, textStatus, errorThrown){
					layer.msg('error!',{icon:1,time:1000});
				}
			});
		
		}
	});
});
</script> 
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>